package org.gvp.generator.core.writer.impl;

import org.gvp.generator.core.WriterToResources;
import org.gvp.generator.core.writer.AbstractFileWriter;
import org.gvp.generator.props.CreateProps;
import org.gvp.generator.props.ProjectProps;
import org.gvp.generator.table.TableInfo;

import java.nio.file.Path;
import java.nio.file.Paths;

@WriterToResources
public class TsReduxActionFileWriter extends AbstractFileWriter {
    @Override
    public void execute(TableInfo tableInfo, ProjectProps project) {
        String upName = tableInfo.getUpperName();
        String imps = """
                import { createAsyncThunk } from "@reduxjs/toolkit";
                import Request from "http/Request";
                import {PagingData, PagingResult} from "type/Page";
                import {%s} from "type/entity/%s";
                """.formatted(upName,upName);
        String prefix = project.getRequestMappingPrefix() == null ? "" : project.getRequestMappingPrefix() + "/";
        String fetchUserPageData = """
                /** 请求%s分页数据 */
                export const fetch%sPageData = createAsyncThunk(
                    '%s/fetch%sPageData',
                    async (paging: PagingData) => {
                        return await Request.get<PagingResult<%s>>("/%s%s/page/" + paging.page + "/" + paging.pageSize);
                    }
                );
                """.formatted(tableInfo.getRemark(),upName,tableInfo.getLowerName(),upName,upName,prefix,tableInfo.getTableName());
        String fetchAddUser = """
                /** 添加%s数据 */
                export const fetchAdd%s = createAsyncThunk(
                    '%s/fetchAdd%s',
                    async (vo: %s) => {
                       return await Request.put<number>("/%s%s", vo);
                    }
                );
                """.formatted(tableInfo.getRemark(),upName,tableInfo.getLowerName(),upName,upName,prefix,tableInfo.getTableName());
        String fetchUpdateUser = """
                /** 更新%s数据 */
                export const fetchUpdate%s = createAsyncThunk(
                    '%s/fetchUpdate%s',
                    async (vo: %s) => {
                        return await Request.post<number>("/%s%s", vo);
                    }
                );
                """.formatted(tableInfo.getRemark(),upName,tableInfo.getLowerName(),upName,upName,prefix,tableInfo.getTableName());
        String fetchDeleteUser = """
                /** 删除%s数据 */
                export const fetchDelete%s = createAsyncThunk(
                    '%s/fetchDelete%s',
                    async (id: number) => {
                        return await Request.delete<number>("/%s%s" + id);
                    }
                );
                """.formatted(tableInfo.getRemark(),upName,tableInfo.getLowerName(),upName,prefix,tableInfo.getTableName());
        channel.writeNewline(imps);
        channel.writeNewline(fetchUserPageData);
        channel.writeNewline(fetchAddUser);
        channel.writeNewline(fetchUpdateUser);
        channel.writeNewline(fetchDeleteUser);

    }

    @Override
    public boolean isWriterFile(CreateProps create) {
        return create.isCreateTsRedux();
    }

    @Override
    public Path getFilePath(String basePath, ProjectProps project, String fileName) {
        return Paths.get(basePath,project.getTypescriptDirPath(),fileName + "AsyncAction.tsx");
    }
}
